package Test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import junit.framework.TestCase;

import org.junit.Test;

import business.Accounts;
import business.Schedule;
import controller.WTAAction;
import data.Job;
import data.Volunteer;


/**
 * This is WTAAction test class that is mostly dependent on the 
 * user input therefore the main test is to verify if 
 * the console behaves as expected. 
 * 
 * The methods used in the WTAAction have been already tested
 * in its individual classes hence, the main test for the 
 * WTAAction class is to ensure that correct and valid information
 * is passed through.
 * 
 * @author Krisnil Gounder
 * @version March 5th, 2011
 *
 */
public class WTAActionTest extends TestCase {
	/**
	 * wtaTest.
	 * 
	 * This field is rarely used in this test class because
	 * WTAAction is highly dependent on the console therefore, 
	 * the main method to test is the "userInput" method, if this method works
	 * correctly it means correct selection is being made and now it all
	 * depends if other individual classes work correctly when called upon.
	 * 
	 * 
	 */
	WTAAction wtaTest = new WTAAction("acc.txt");

	
	/**
	 * This test case method validates the integer user input from the console and
	 * verifies that the number selected by the user is within the range or valid 
	 * input as specified by the program.
	 * 
	 * Enter 0 to verify the console input works.
	 * 
	 */
	@Test
	public void testUserInput() {

//		assertEquals("User input 0", wtaTest.userInput(), 0);
//		assertEquals("User input any positive number within option range", wtaTest.userInput(), 1);
//		assertEquals("User input any positive number out of option range", wtaTest.userInput(), 100);
//		assertEquals("User input any negative number is considered invalid -3", wtaTest.userInput(), -3);
//		assertEquals("User input non integer is considered invalid", wtaTest.userInput(), 1.1);
	}
	
	
	/**
	 * Check if file loads properly.
	 * The load file method is critical because it ensures that
	 * the correct data is loaded upon each time the program is 
	 * executed and that previous data is preserved.
	 * 
	 * author Faiz
	 */
	@Test
	public void testLoadFile() throws IOException {
		File writeFile = new File("acc.txt");
		BufferedWriter writer = new BufferedWriter(new FileWriter(writeFile));
		String test = "Volunteer,,jane,,andalas,,janead,,spring,,2042442234,,janead@gmail.com,,07/24/1977,,false\n" +
		"Volunteer,,shawn,,weaver,,weav,,wild42,,20423423423,,shawn@gmail.com,,09/05/1966,,false\n" +
		"Volunteer,,snoha,,grace,,sgrace,,season,,2042322324,,grace1@gmail.com,,10/05/1986,,false\n" +
		"Scheduler,,deeds,,adam,,23223232342,,deed@washington.edu\n" +
		"Scheduler,,james,,coolant,,2042322421,,mnm@yahoo.com\n" +
		"Scheduler,,smith,,fast22,,5012425324,,scheduler21@trail.org\n" +
		"Scheduler,,schUsername,,schPassword,,schPhone,,schEmail\n";
		String jobtest = "Job,,CleanTrack,,204234323,,Seattle,,5,,5,,Cleanup,,03/25/2011,,01:30PM,,Funland,,1,,true,,true\n" +
			"Job,,TacomaTrail,,2442422444,,Tacoma,,2,,2,,Cleanup,,03/25/2011,,03:30PM,,Greenpark,,2,,true,,true\n" +
			"Job,,tjob,,2423424244,,faizsplace,,40,,10,,clean,,010101,,12,,testorg,,1000,,true,,false\n" +
			"Job,,fakename,,2424435235,,fakeloaction,,44,,3,,fakedescrip,,03/11/2012,,03:01PM,,fakeorg,,7,,true,,false\n";
		writer.write(test + jobtest);
		writer.close();
		WTAAction wtaTest = new WTAAction("acc.txt");
		wtaTest.loadFile();
		assertEquals(wtaTest.accounts.toString(), test);
		assertEquals(wtaTest.schedule.toString(), jobtest);
	}
	
	
	
	/**
	 * Test if a unregistered user registers then a new Volunteer object is created
	 * which is then added to accounts.
	 * 
	 * Furthermore, this method tests if the correct information is passed through
	 * to create the new account.
	 * 
	 */
	@Test
	public void testUnregisteredUser() {
		Accounts accounts = new Accounts();
		String name = "testName";
		String lastName = "testLastName";
		String newUsername = "testUsername";
		String newPassword = "testPassword";
		String newPhone = "testPhone";
		String emailAddress = "testEmailAddress";
		String dob = "testDob";
		
		accounts.addAccount(new Volunteer(name, lastName, newUsername,
				newPassword, newPhone, emailAddress, dob, false));
		
		for (Volunteer theVolunteer : accounts.getVolunteerList()) {
			assertEquals("Check if correct name was passed", theVolunteer.getFirstname(), "testName");
			assertEquals("Check if correct last name was passed", theVolunteer.getSurname(), "testLastName");
			assertEquals("Check if correct username was passed", theVolunteer.getUsername(), "testUsername");
			assertEquals("Check if correct password was passed", theVolunteer.getPassword(), "testPassword");
			assertEquals("Check if correct phone was passed", theVolunteer.getPhone(), "testPhone");
			assertEquals("Check if correct email address was passed", theVolunteer.getEmail(), "testEmailAddress");
			assertEquals("Check if correct dob was passed", theVolunteer.getDob(), "testDob");
		}
	}
		
		/**
		 * This method is a test that when a LMA requests for a job the 
		 * entered information is passed through the Job class correctly.
		 * 
		 * Once, the information is passed, by default the Job is not 
		 * verified hence, this test method sets the verification by the Scheduler
		 * to be true and now the verification field is true.
		 * 
		 */
		@Test
		public void testLMA_Scheduler() {
			Schedule schedule = new Schedule();
			ArrayList<Volunteer> loadedVolunteers = new ArrayList<Volunteer>();

			String lmaName = "testLmaName";
			String lmaPhone = "24234342323";
			String jobName = "testJobName";
			String trailLocation = "testTrailLocation";
			int grade = 4;
			int difficulty = 2;			
			String description = "testDescription";
			String date = "03/11/2011";
			String time = "01:03PM";
			int size = 6;
			
			schedule.addJob(new Job(jobName,lmaPhone, trailLocation, grade,
					difficulty, description, date, time, lmaName, size,
					false, false, loadedVolunteers));
			
			for (Job theJob : schedule.viewUnscheduledJob()) {
				assertEquals("Test Job LMA name", theJob.getLMA(), lmaName);
				assertEquals("Test Job name", theJob.getName(), jobName);
				assertEquals("Test Job location", theJob.getLocation(), trailLocation);
				assertEquals("Test Job grade", theJob.getGrade(), grade);
				assertEquals("Test Job difficulty", theJob.getDifficulty(), difficulty);
				assertEquals("Test Job description", theJob.getDescription(), description);
				assertEquals("Test Job date", theJob.getDate(), date);
				assertEquals("Test Job time", theJob.getTime(), time);
				assertEquals("Test Job size", theJob.getJobSize(), size);
				assertEquals("Test Job verification", theJob.getVerified(), false);
					
				// Test if Scheduler verifies the Job
				theJob.setVerified(true);
				assertEquals("Test Job verification", theJob.getVerified(), true);

			}
	}
	
	
	
}
